<?php

class UserController extends \BaseController {
    protected $layout = 'layouts.admin';

    public function __construct (User $user) {
        parent::__construct();
        $this->user = $user;
    }
    
    /**
     * check login with admin
     * @author Thuan Truong
     */
    public function adminLogin () {
        $input = array_map('trim', Input::all());   //trim input
            
        $v = $this->user->validateLogin($input);
        if ($v->passes()) {
            $check_user = $this->user->userLogin($input['email'], $input['password']);
            
            if ($check_user == 0) {
                return Response::json(array('status' => 0,  'code' => 'invalid', 'message' => 'valid'));
            } elseif ($check_user == 1) {
                $userEmail = $this->user->findUserbyEmail($input['email']);
                $user = User::find($userEmail[0]->id);
                Sentry::login($user, false);
            }
            if (BaseController::getRole() == 1 || BaseController::getRole() == 2) {
                return Response::json(array('status' => 1, 'redirect' => route('admin.panel'), 'message' => '123'));
            } else {
                return Response::json(array('status' => 1, 'redirect' => route('home'), 'message' => Lang::get('user.action.login.success')));
            }
        } else {
            if (Request::ajax()) {
                $arr_error = $v->messages()->getMessages();
                return Response::json(array(
                    'status' => 0, 
                    'code' => 'invalid_data', 
                    'messages' => $arr_error));
            } else {
                return Redirect::action('UserController@index');
            }
        }
    }
    
    /**
     * Display a Users list
     * @author Thuan Truong
     * @return Response
     */
    public function index()
    {
        if (! Sentry::getUser()->hasAccess('user.view')) {
            App::abort(404);
        }
        $users = $this->user->getAllUser();

        $view = View::make('user.index')->with(array(
            'users' => $users,
            'curUser' => Sentry::getUser(),
            'pageSize' => $this->pageSize
        ));
        if (Request::ajax()) {
            return $this->ajaxResponse($view, Lang::get('nav.page.user.index'));
        } else {
            $this->layout->content = $view;
        }
    }
    
    
    /**
     * View form create
     * @author Thuan Truong
     */
    public function create() {
        $view = View::make('user.create');
        if (Request::ajax()) {
            return $this->ajaxResponse($view, Lang::get('nav.page.user.create'));
        } else {
            $this->layout->content = $view;
        }
    }
    
    
    /**
     * Show the form for editing the specified user.
     * @author Thuan Truong
     */
    public function edit($id) {
        $user = User::find($id);

        if (empty($user)) {
            return Redirect::action('UserController@index');
        }

        $view = View::make('user.edit')->with(compact('user'));
        if (Request::ajax()) {
            return $this->ajaxResponse($view, Lang::get('nav.page.user.edit'));
        } else {
            $this->layout->content = $view;
        }
    }
    
    
    /**
     * Update Status User
     * @author Thuan Truong
     */
    public function updateStatus($id) {
        $user = User::find($id);
        if ($user) {
            $is_banner = $user->is_banner;
            if ($is_banner == 1) {
                $user->is_banner = 0;
                $user->update();
            } else {
                $user->is_banner = 1;
                $user->update();
            }
            
            return Response::json(array(
                'status' => 'SUCCESS',
            ));
            
        } else {
            return Response::json(array(
                'status' => 'ERROR',
            ));
        }
        
    }

	/**
	 * sign up user page
	 * @author Thuan Truong
	 */
	public function signup (){
		$this->layout = View::make('layouts.application');
		$view = View::make('user.signup')->with(array(
        ));
        
        $this->layout->content = $view;
	}
	
	/**
	 * sign up user page
	 * @author Thuan Truong
	 */
	public function signupLaborer (){
		$this->layout = View::make('layouts.application');
		$view = View::make('user.signup_laborer')->with(array(
        ));
        
        $this->layout->content = $view;
	}
	
	/**
	 * store user when signup page
	 * @author Thuan Truong
	 */
	public function signupAccount () {
		$input = array_map('trim', Input::all());   
        $v = $this->user->validateRegister($input);
        
        if($v->passes()){
            $secret_key = $this->getSecretKey();
            $activeCode  = $this -> getActiveCode();           
            $this->user->registerUser($input, $activeCode, $secret_key);            
            $this->sendActiveCode($input['email'], $input['full_name'], $activeCode, $secret_key);
            
            Session::flash('f_notice', Lang::get('user.action.register.done'));  
            if (!empty($input['admin_hidden']))
                return Response::json(array( 'status' => 1, 'redirect' => route('admin.user.index'), 'message' => Lang::get('user.action.register.done') ));
            else
                return Response::json(array( 'status' => 1, 'redirect' => route('home'), 'message' => Lang::get('user.action.register.done') ));
            
        }else{
            if (Request::ajax()) {
                $arr_error = $v->messages()->getMessages();
                return Response::json(array( 'status' => 0,  'code' => 'invalid_data', 'messages' => $arr_error));
            }else{
                return Redirect::action('UserController@index');
            }
        }
	}
}